package com.youi.web.controller;

import com.youi.pojo.dto.AddressDto;
import com.youi.pojo.dto.BaseResponseEntity;
import com.youi.entity.Address;
import com.youi.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author: xxf
 * @CreateDate: 2020/10/29 17:36
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 根据用户ID查询地址信息
     * @param uid
     * @return
     */
    @RequestMapping(value = "/info/{uid}",method = RequestMethod.GET)
    public BaseResponseEntity<List<Address>> findAddress(@PathVariable String uid) {
        BaseResponseEntity<List<Address>> responseEntity = new BaseResponseEntity<>();
        //调用service层获取地址信息
        List<Address> list = addressService.selectByUId(uid);
        //对查询该uid用户的地址信息进行判断
        if (list != null) {
            responseEntity.setData(list);
            responseEntity.setMsg("获取用户收货地址信息成功");
            responseEntity.setStatus(200);

        } else {
            responseEntity.setData(null);
            responseEntity.setMsg("无此用户收货地址信息");
            responseEntity.setStatus(404);
        }

        return responseEntity;
    }

    /**
     * 添加收货地址
     * @param addressDto
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponseEntity<List<Address>> saveAddress(@RequestBody AddressDto addressDto) {
        BaseResponseEntity<List<Address>> responseEntity = new BaseResponseEntity<>();
        //获取添加地址信息
        int res = addressService.insert(addressDto);

        if (res > 0) {
            //添加成功后获取该用户的所有地址信息给前端展示
            responseEntity.setMsg("添加地址信息成功");
            responseEntity.setStatus(200);

            responseEntity.setData(addressService.selectByUId(addressDto.getUId().toString()));

        } else {
            //添加失败
            responseEntity.setData(null);
            responseEntity.setMsg("添加失败，请与管理员联系");
            responseEntity.setStatus(400);
        }
        return responseEntity;
    }


}
